package com.shop.shopserver.controller.user;

import com.shop.shopcommon.result.Result;
import com.shop.shoppojo.dto.AddressDTO;
import com.shop.shoppojo.entity.Address;
import com.shop.shoppojo.vo.AddressVO;
import com.shop.shopserver.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/address")
@RequiredArgsConstructor
@Slf4j
@Api(tags = "地址模块")
public class AddressController {
    @Autowired
    private AddressService addressService;

    /**
     * 新增或者编辑地址
     * @param addressDTO
     * @return
     */


    @PostMapping("/addorupdate")
    @ApiOperation(value = "新增或者编辑地址")
    public Result save(@RequestBody AddressDTO addressDTO) {
        if (addressDTO.getId() == null) {

            addressService.save(addressDTO);
            return Result.success(null,"新增成功");
        }
        addressService.update(addressDTO);
        return Result.success(null,"更新成功");
    }


    /**
     * 获取用户地址
     * @return
     */
    @GetMapping("/list")
    @ApiOperation(value = "获取用户地址")
    public Result<List<AddressVO>> list() {
        List<AddressVO> list = addressService.list();
        return Result.success(list, "查询成功");
    }


    /**
     * 根据id删除地址
     * @param id
     * @return
     */

    @DeleteMapping("/del/{id}")
    @ApiOperation(value = "据id删除地址")
    public Result deleteById(@PathVariable Long id) {
        addressService.delete(id);
        return Result.success(null, "删除成功");
    }

    /**
     * 设置默认地址
     * @param addressDTO
     * @return
     */
    @PutMapping("default")
    @ApiOperation(value = "设置默认地址")
    public Result setDefault(@RequestBody AddressDTO addressDTO) {
        addressService.setDefault(addressDTO);
        return Result.success(null, "设置成功");
    }



}
